package com.hippo.core.ui.compose.spt

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.ui.components.PdfWatcher
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.DownloadVM

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UserManualScreen(onBack: (() -> Unit),vm:DownloadVM = viewModel()) {

    val uiState = vm.uiState

    val context = LocalContext.current

    if(uiState.isDownloading){
        DownloadPdf(
            onDismissRequest = {
                vm.updateUiState(uiState.copy(isDownloading = false))
            },
            modifier = Modifier.fillMaxWidth(),
            progress = uiState.downloadProgress
        )
    }

    Column(modifier = Modifier
        .fillMaxSize()
    ) {

        TopAppBar(
            modifier = Modifier.background(
                color = Color.White
            ),
            navigationIcon = {
                IconButton(onClick = onBack) {
                    Icon(Icons.AutoMirrored.Filled.ArrowBack, null)
                }
            },
            title = {
                Text(
                    text =  stringResource(R.string.spt_1),
                    textAlign = TextAlign.Center,
                    fontSize = 18.sp,
                    modifier = Modifier.fillMaxWidth(),
                    color = Color.Black
                )
            },
            actions = {
                IconButton(onClick = {
                    vm.downloadPdf(context)
                }) {
                    Image(
                        painter = painterResource(id = R.mipmap.download_s),
                        contentDescription = "User Manual",
                        modifier = Modifier.size(25.dp)
                    )
                }
            }
        )

        HorizontalDivider(thickness = 0.5.dp)

        Column(modifier = Modifier
            .fillMaxWidth()
            .paddingBody()) {

            PdfWatcher()
        }

    }

}


@Composable
fun DownloadPdf(onDismissRequest: () -> Unit,progress:Int,modifier: Modifier = Modifier) {

    Dialog(
        onDismissRequest = onDismissRequest
    ) {
        Column(
            modifier = modifier
                .fillMaxWidth()
                .background(color = Color.White, shape = MaterialTheme.shapes.medium)
                .padding(16.dp)
        ) {

            Text(
                text = stringResource(R.string.spt_3),
                fontSize = 14.sp,
                modifier = Modifier
                    .align(Alignment.CenterHorizontally)
                    .padding(10.dp),
            )

            LinearProgressIndicator(
                modifier = modifier,
                progress = {
                    progress.toFloat() / 100
                },
                color = MaterialTheme.colorScheme.primary,
                trackColor = Color(0xFFECECEC),
            )

            val progressText = if(progress <100){
                "${progress}%"
            }else{
                stringResource(R.string.spt_10)
            }

            Text(
                text = progressText,
                fontSize = 14.sp,
                modifier = Modifier
                    .padding(10.dp)
                    .align(Alignment.CenterHorizontally),
            )

        }
    }

}


@Preview(showBackground = true)
@Composable
private fun UserManualScreenPreview() {
    HippoTheme {
        UserManualScreen(
            onBack = {},
        )
    }
}

@Preview(showBackground = true)
@Composable
private fun DownloadBookPreview() {
    HippoTheme {
        DownloadPdf(
            onDismissRequest = {}
            ,progress = 30,modifier = Modifier.fillMaxWidth())
    }
}


